package com.module.overview.ui.editCardList

import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：13/6/2025
 *
 * 描述：实现RecyclerView拖拽排序的ItemTouchHelper回调
 *
 * 修订历史：
 *
 */
class ItemTouchHelperCallback(private val adapter: ItemTouchHelperAdapter) : ItemTouchHelper.Callback() {

    override fun isLongPressDragEnabled(): Boolean {
        // 我们使用自定义的拖拽触发方式，所以这里返回false
        return false
    }

    override fun isItemViewSwipeEnabled(): Boolean {
        // 不需要滑动删除功能
        return false
    }

    override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
        // 设置拖拽方向为上下
        val dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN
        return makeMovementFlags(dragFlags, 0)
    }

    override fun onMove(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        target: RecyclerView.ViewHolder
    ): Boolean {
        // 调用适配器的方法来处理项目移动
        return adapter.onItemMove(viewHolder.bindingAdapterPosition, target.bindingAdapterPosition)
    }

    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
        // 不需要实现，因为我们禁用了滑动功能
    }

    override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
        // 当项目被选中进行拖拽时，可以在这里添加视觉反馈
        if (actionState == ItemTouchHelper.ACTION_STATE_DRAG && viewHolder != null) {
            viewHolder.itemView.alpha = 0.7f
        }
        super.onSelectedChanged(viewHolder, actionState)
    }

    override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
        super.clearView(recyclerView, viewHolder)
        // 当拖拽结束时，恢复项目的视觉状态
        viewHolder.itemView.alpha = 1.0f
    }
}